package game.behaviours;

import edu.monash.fit2099.engine.actions.Action;
import edu.monash.fit2099.engine.actions.MoveActorAction;
import edu.monash.fit2099.engine.actors.Actor;
import edu.monash.fit2099.engine.actors.Behaviour;
import edu.monash.fit2099.engine.positions.Exit;
import edu.monash.fit2099.engine.positions.GameMap;
import edu.monash.fit2099.engine.positions.Location;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 漫游行为：随机选择一个可进入的相邻格并移动；若无可移动方向则返回 null。
 */
public class WanderBehaviour implements Behaviour {

    @Override
    public Action generateAction(Actor actor, GameMap map) {
        Location here = map.locationOf(actor);
        List<Exit> exits = here.getExits();
        List<Exit> candidates = new ArrayList<>();

        for (Exit exit : exits) {
            Location dest = exit.getDestination();
            if (dest.canActorEnter(actor)) {
                candidates.add(exit);
            }
        }

        if (candidates.isEmpty()) {
            return null;
        }

        Exit choice = candidates.get(ThreadLocalRandom.current().nextInt(candidates.size()));
        return new MoveActorAction(choice.getDestination(), choice.getName());
    }
}
